/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp.search.common;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.example.TravelApp.R;
import com.example.TravelApp.constants.MapConstants;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

/**
 * This class is provides an array adapter for the place object and implements
 * the MapConstaants.
 * 
 * @author Haritha Subramanyam, Mani Brundha
 * 
 */

public class GPlaceObjectActivity extends ArrayAdapter<PlaceObject> implements
		MapConstants {
	private final Activity context;
	private final List<PlaceObject> list;

	public GPlaceObjectActivity(Activity context, List<PlaceObject> results) {
		super(context, R.layout.main, results);
		this.context = context;
		this.list = results;

	}

	/**
	 * This class acts as a view holder for the place object. Each view is
	 * constructed such that it captures all the details of the place object
	 * 
	 * @author Haritha Subramanyam, Mani Brundha
	 * 
	 */
	static class ViewHolder {
		protected TextView venueTextView, addressTextView, urlTextView,
				optionNumberView;
		protected RatingBar ratingbarView;
		protected ImageView ImageView01;
	}

	/**
	 * Constructs a view and returns it.
	 */
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View rowView = null;
		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			rowView = inflator.inflate(R.layout.interest_details, null);
			final ViewHolder viewHolder = new ViewHolder();
			// viewHolder.optionNumberView = (TextView)
			// rowView.findViewById(R.id.option);
			viewHolder.venueTextView = (TextView) rowView
					.findViewById(R.id.venuName);
			viewHolder.addressTextView = (TextView) rowView
					.findViewById(R.id.myaddress);
			viewHolder.urlTextView = (TextView) rowView.findViewById(R.id.url);
			viewHolder.ratingbarView = (RatingBar) rowView
					.findViewById(R.id.ratingbar1);
			viewHolder.ImageView01 = (ImageView) rowView
					.findViewById(R.id.ImageView01);
			rowView.setTag(viewHolder);
		} else {
			rowView = convertView;
		}

		ViewHolder holder = (ViewHolder) rowView.getTag();
		if (list.get(position).getIsVisited() == true) {
			rowView.setBackgroundColor(Color.GRAY);
		} else {
			rowView.setBackgroundColor(Color.BLACK);
		}
		holder.venueTextView.setText(list.get(position).getVenueName());
		holder.addressTextView.setText(list.get(position).getAddress());
		holder.ratingbarView.setRating(Float.parseFloat(list.get(position)
				.getRating()));
		String url = "";
		try {

			url = list.get(position).getIconURLStr();
			if (url == "" || url == null) {
				url = GOOGLE_PLACES_DEFAULT_IMG_URL;
			}
			Drawable image = ImageOperations(this.getContext(), url);
			holder.ImageView01.setImageDrawable(image);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return rowView;
	}

	public Object fetch(String address) throws MalformedURLException,
			IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;
	}

	private Drawable ImageOperations(Context ctx, String url) {
		try {

			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			System.out.println("MalformedURLException");
			return null;
		} catch (IOException e) {
			System.out.println("IOException");
			return null;
		}
	}

}